package com.cc.crm.controller;

import com.cc.crm.pojo.UserInfo;
import com.cc.crm.service.UserInfoService;
import com.cc.crm.vo.UserInfoVo;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program:
 * @ClassName:
 * @description:
 * @author: zgc
 * @date:
 * @Version 1.0
 **/
@Controller
@RequestMapping("/user")
@Slf4j
public class UserInfoController {
    @Resource
    private UserInfoService userInfoService;
    
    //跳转到登录页面
    @RequestMapping("/toLogin")
    public String toLogin(HttpSession session){
        session.invalidate();
        return "login";
    }
    
    //跳转到注册页面
    @RequestMapping("/toRegister")
    public String toRegister(){
        return "register";
    }
    
   
    //查询用户
    @RequestMapping("/selectUserInfo")
    @ResponseBody
    public PageInfo<UserInfo> selectUserInfo(UserInfoVo userInfoVo, HttpSession httpSession){
        PageInfo<UserInfo> pageInfo=null;
        if (userInfoVo.getAccount()=="" && userInfoVo.getPassword()==""){
            return pageInfo;
        }
        pageInfo = userInfoService.selectUserInfo(userInfoVo);
        if (pageInfo.getList() != null && httpSession.getAttribute("username") == null){
            httpSession.setAttribute("username",pageInfo.getList().get(0).getUsername());
            httpSession.setAttribute("userid",pageInfo.getList().get(0).getId());
            httpSession.setAttribute("usertype",pageInfo.getList().get(0).getUsertype());
        }
        return pageInfo;
    }

    //查询用户是否已存在
    @RequestMapping("/repeatAccount")
    @ResponseBody
    public HashMap<String,Boolean> repeatAccount(UserInfoVo userInfoVo){
        PageInfo<UserInfo> pageInfo = userInfoService.selectUserInfo(userInfoVo);
        HashMap<String,Boolean> map = new HashMap<>();
        if(pageInfo.getList().isEmpty()){
            map.put("valid",true);
        }else{
            map.put("valid",false);
        }
        return map;
    }
    
    //添加用户
    @RequestMapping("/insertUserInfo")
    @ResponseBody
    public int insertUserInfo(@Valid UserInfo userInfo, BindingResult result) {
        if (result.hasErrors()) {
            //如果有验证错误，处理错误信息
            List<FieldError> errors = result.getFieldErrors();
            for (FieldError error : errors) {
                System.out.println("出错的属性是：" + error.getField() + "提示信息：" + error.getDefaultMessage());
            }
            return 0;
        }
        if(userInfo.getEmail()==""){
            userInfo.setEmail(null);
        }
        int backValue = userInfoService.insertUserInfo(userInfo);
        return backValue;
    }
    
    //修改用户信息
    @RequestMapping("/updateUserInfo")
    @ResponseBody
    public int updateUserInfo(UserInfo userInfo){
        int backValue = userInfoService.updateUserInfo(userInfo);
        return backValue;
    }
    
    //逻辑删除用户信息
    @RequestMapping("/deleteUserInfo")
    @ResponseBody
    public int deleteUserInfo(UserInfo userInfo){
        int backValue = userInfoService.deleteUserInfo(userInfo);
        return backValue;
    }
}
